package cn.edu.suda.schoolBus.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.suda.schoolBus.bean.DepartureInfo;
import cn.edu.suda.schoolBus.dao.DepartureInfoMapper;
import cn.edu.suda.schoolBus.service.DepartureInfoService;
import cn.edu.suda.schoolBus.util.ResultUtil;
import cn.edu.suda.schoolBus.util.ScheduleUtil;

@Service
public class DepartureInfoServiceImpl implements DepartureInfoService {

	@Autowired
	private DepartureInfoMapper departureInfoDao;

	@Override
	public int addDepartureInfo(DepartureInfo departureInfo) {
		return departureInfoDao.insertSelective(departureInfo);
	}

	@Override
	public List<DepartureInfo> getDepartureInfo(int dcnId) {
		return departureInfoDao.selectDepartureInfo(dcnId);
	}

	@Override
	public Map<String, Object> statisticsSchoolBus(int days) {
		Date firstDate = new Date();
		Date secondDate = ResultUtil.getDate(days);
		List<DepartureInfo> departureInfos = departureInfoDao.statisticsSchoolBus(firstDate, secondDate);
		Map<String, Object> map = getMap(departureInfos);
		return map;
	}

	private Map<String, Object> getMap(List<DepartureInfo> departureInfos) {
		List<Integer> numbers = new ArrayList<Integer>();
		List<String> sbNumbers = ScheduleUtil.getSchoolBusNumber();
		for (int i = 0; i < sbNumbers.size(); i++) {
			numbers.add(0);
		}
		for (DepartureInfo departureInfo : departureInfos) {
			int index = sbNumbers.indexOf(departureInfo.getSchoolBus().getSbNumber());
			numbers.set(index, departureInfo.getRelateToSchoolBus());
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sbNumbers", sbNumbers);
		map.put("numbers", numbers);
		return map;
	}

}
